local longlue = fk.CreateSkill {
  name = "kl__longlue",
  max_branches_use_time = {
    ["active"] = {
      [Player.HistoryPhase] = 1
    },
  }
}

--一个主动技3个效果，还有次数计算，可以多参考参考。

Fk:loadTranslationTable{
  ["kl__longlue"] = "龙略",
  [":kl__longlue"] = "出牌阶段限一次，你可以失去1点体力展示一种类型手牌且根据展示的牌的类型执行以下效果直到本轮结束："..
  "<br>1.基本牌：使用【杀】次数+1，使用【桃】可以摸一张牌，使用【酒】可以弃置其他角色一张牌，可以重铸【闪】；"..
  "<br>2.锦囊牌：可以把任意锦囊牌当【决斗】使用；"..
  "<br>3.装备牌：当你使用装备牌时，你可以摸一张牌，本轮限一次，当你被【杀】或伤害类锦囊牌指定时，可以弃置任意牌摸等量的牌，若你以此法弃置了所有手牌，你多摸一张牌。",

  ["#kl__longlue"] = "龙略：展示一种类型的手牌，根据类型获得对应效果直到本轮结束，或根据选项执行对应操作",
  ["@kl__longlue-round"] = "龙略",
  ["kl__longlue-jink"] = "重铸【闪】",
  ["kl__longlue-trick"] = "将锦囊牌当【决斗】使用",

  ["$kl__longlue1"] = "豪情龙枪随义胆，敢教英雄伏忠魂。",
  ["$kl__longlue2"] = "单骑战破万马群，长坂扬威映斜曛。",
}

longlue:addEffect("active", {
  mute = true,
  prompt = "#kl__longlue",
  can_use = function(self, player)
    if table.contains(player:getTableMark("@kl__longlue-round"), "基本") or table.contains(player:getTableMark("@kl__longlue-round"), "锦囊") then
      return true
    else
      return player.phase == Player.Play and longlue:withinBranchTimesLimit(player, "active", Player.HistoryPhase)
    end
  end,
  min_card_num = function (self, player)
    if self.interaction.data == "kl__longlue-jink" or self.interaction.data == "kl__longlue-trick" then
      return 1
    else
      return 0
    end
  end,
  max_card_num = function (self, player)
    if self.interaction.data == "kl__longlue-jink" then
      return 99
    elseif self.interaction.data == "kl__longlue-trick" then
      return 1
    else
      return 0
    end
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    if self.interaction.data == "kl__longlue-jink" then
      return Fk:getCardById(to_select).trueName == "jink"
    elseif self.interaction.data == "kl__longlue-trick" then
      return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeTrick
    else
      return false
    end
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if self.interaction.data == "kl__longlue-trick" then
      return #selected == 0 and to_select ~= player and player:canUseTo(Fk:cloneCard("duel"), to_select)
    else
      return false
    end
  end,
  min_target_num = function (self, player)
    if self.interaction.data == "kl__longlue-trick" then
      return 1
    else
      return 0
    end
  end,
  max_target_num = function (self, player)
    if self.interaction.data == "kl__longlue-trick" then
      return 1
    else
      return 0
    end
  end,
  interaction = function (self, player)
    local all_choices, choices = {"basic", "trick", "equip", "kl__longlue-jink", "kl__longlue-trick"}, {}
    if longlue:withinBranchTimesLimit(player, "active", Player.HistoryPhase) then
      if table.find(player:getCardIds("h"),function(id) return Fk:getCardById(id).type == Card.TypeBasic end) then
        table.insert(choices, "basic")
      end
      if table.find(player:getCardIds("h"),function(id) return Fk:getCardById(id).type == Card.TypeTrick end) then
        table.insert(choices, "trick")
      end
      if table.find(player:getCardIds("h"),function(id) return Fk:getCardById(id).type == Card.TypeEquip end) then
        table.insert(choices, "equip")
      end
    end
    if table.contains(player:getTableMark("@kl__longlue-round"), "基本") then
      table.insert(choices, "kl__longlue-jink")
    end
    if table.contains(player:getTableMark("@kl__longlue-round"), "锦囊") then
      table.insert(choices, "kl__longlue-trick")
    end
    return UI.ComboBox{ choices = choices, all_choices = all_choices }
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    if not self.interaction.data then return end
    player:broadcastSkillInvoke(longlue.name)
    if self.interaction.data == "kl__longlue-jink" then
      room:notifySkillInvoked(player, longlue.name, "drawcard")
      room:recastCard(effect.cards, player, longlue.name)
    elseif self.interaction.data == "kl__longlue-trick" then
      room:notifySkillInvoked(player, longlue.name, "offensive")
      room:useVirtualCard("duel", effect.cards, player, effect.tos, longlue.name)
    else
      player:addSkillBranchUseHistory(longlue.name, "active", 1)
      room:notifySkillInvoked(player, longlue.name, "control")
      room:loseHp(player, 1, longlue.name)
      if player.dead then return end
      local cards = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id):getTypeString() == self.interaction.data
      end)
      player:showCards(cards)
      if self.interaction.data == "basic" then
        room:addTableMarkIfNeed(player, "@kl__longlue-round", "基本")
      elseif self.interaction.data == "trick" then
        room:addTableMarkIfNeed(player, "@kl__longlue-round", "锦囊")
      elseif self.interaction.data == "equip" then
        room:addTableMarkIfNeed(player, "@kl__longlue-round", "装备")
      end
    end
  end,
})

longlue:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase and table.contains(player:getTableMark("@kl__longlue-round"), "基本") then
      return 1
    end
  end,
})

longlue:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(longlue.name) and data.card then
      return (table.contains(player:getTableMark("@kl__longlue-round"), "装备") and data.card.type == Card.TypeEquip)
      or (table.contains(player:getTableMark("@kl__longlue-round"), "基本") and (data.card.trueName == "peach" or data.card.trueName == "analeptic"))
    end
  end,
  on_cost = function(self, event, target, player, data)
    if data.card.trueName == "peach" or data.card.type == Card.TypeEquip then
      return player.room:askToSkillInvoke(player, {
        skill_name = longlue.name,
        prompt = "功威：是否摸一张牌？",
      })
    end
    if data.card.trueName == "analeptic" then
      local tos = table.filter(player.room:getOtherPlayers(player), function (p)
        return not p:isNude()
      end)
      if #tos == 0 then return false end
      local to = player.room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = tos,
        cancelable = true,
        skill_name = longlue.name,
        prompt = "功威：你可以弃置一名其他角色一张牌",
      })
      if #to > 0 then
        event:setCostData(self, { to = to[1] })
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    if data.card.trueName == "peach" or data.card.type == Card.TypeEquip then
      player:drawCards(1, longlue.name)
    end
    if data.card.trueName == "analeptic" then
      local to = event:getCostData(self).to
      local id = player.room:askToChooseCards(player, {
        min = 1,
        max = 1,
        target = to,
        flag = "he",
        skill_name = longlue.name,
      })
      if #id > 0 then
        player.room:throwCard(id, longlue.name, to, player)
      end
    end
  end,
})

longlue:addEffect(fk.TargetSpecifying, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return data.to == player and player:hasSkill(longlue.name) and data.card and data.card.is_damage_card and
    table.contains(player:getTableMark("@kl__longlue-round"), "装备") and player:usedEffectTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1,
      max_num = 999,
      include_equip = true,
      cancelable = true,
      skill_name = longlue.name,
      prompt = "龙略：你可以弃置任意牌摸等量的牌，若你以此法弃置了所有手牌，你多摸一张牌",
    })
    if #cards > 0 then
      event:setCostData(self, { card = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local hand = player:getCardIds("h")
    local cards = event:getCostData(self).card
    local more = #hand > 0
    for _, id in ipairs(hand) do
      if not table.contains(cards, id) then
        more = false
        break
      end
    end
    player.room:throwCard(cards, longlue.name, player, player)
    if not player.dead then
      player.room:drawCards(player, #cards + (more and 1 or 0), longlue.name)
    end
  end,
})


return longlue